/**
 * 注册：第一步 邮箱注册、密 码、确定密码、真实姓名、生 日、性 别、职业
 */
var userUrl = rootPath + "/user";

$(function() {
			initDate();
			initJob();
		});

// 初始化职业
function initJob() {
	var job = $("#job");
	var jobHtml = "";
	$.ajax({
				url : userUrl + "/workJobInfo",
				type : "POST",
				dataType : "json",
				async : false,
				success : function(response) {
					if (response.success && response.busiSuccess) {
						var data = response.data;
						var jobData;
						for (var i = 0; i < data.length; i++) {
							jobData = data[i];
							jobHtml += "<option value=" + jobData.workJobId
									+ ">" + jobData.workJobName + "</option>";
						}
					} else {
						initDefailJob();
					}
				},
				error : function(response) {
					initDefailJob();
				}
			});
	// 初始化默认职业信息
	function initDefailJob() {
		jobHtml += "<option value=1>学生</option>";
	}
	job.html(jobHtml);
}

// 初始化年月日
function initDate() {
	initYear();
	initMonth();
	initDay();
}

// 初始化天
function initDay() {
	var year = $("#year").find("option:selected").text();
	var month = $("#month").find("option:selected").text();

	var maxMonth;
	if (month == 2) {
		if (((year % 4 == 0) && (year % 100 != 0))
				|| ((year % 4 == 0) && (year % 400 == 0))) {
			maxMonth = 29;
		} else {
			maxMonth = 28;
		}
	} else if (month == 4 || month == 6 || month == 9 || month == 11) {
		maxMonth = 30;
	} else {
		maxMonth = 31;
	}

	var dayHtml = "";
	var day = $("#day");
	for (var i = 1; i <= maxMonth; i++) {
		dayHtml += "<option>";
		dayHtml += i;
		dayHtml += "</option>";
	}
	day.html(dayHtml);
}

// 初始化月
function initMonth() {
	var monthHtml = "";
	var month = $("#month");
	for (var i = 1; i < 13; i++) {
		monthHtml += "<option>";
		monthHtml += i;
		monthHtml += "</option>";
	}
	month.html(monthHtml);
}

// 初始化年
function initYear() {
	var currentYear = new Date().getYear();
	if (currentYear < 1000) {
		currentYear += 1900;
	}
	var oneHundredBefore = currentYear - 100;

	var yearHtml = "";
	var year = $("#year");
	for (var i = currentYear; i > oneHundredBefore; i--) {
		yearHtml += "<option>";
		yearHtml += i;
		yearHtml += "</option>"
	}
	year.html(yearHtml);
}

// 用户注册
function regist1() {
	var hasReaded = $('#hasReaded').attr("checked");

	// 验证用户是否阅读并同意服务条款
	if (hasReaded == "checked") {
		// 注册邮箱
		var email = $('#email').val();
		// 密码
		var userPwd = $('#userPwd').val();
		// 确定密码
		var userPwdConfirm = $('#userPwdConfirm').val();
		// 真实姓名
		var realName = $('#realName').val();
		// 生日
		var year = $("#year").find("option:selected").text();
		var month = $("#month").find("option:selected").text();
		var day = $("#day").find("option:selected").text();
		var birthday = year + "-";
		if (month < 10) {
			birthday += "0" + month;
		} else {
			birthday += month;
		}
		birthday += "-";
		if (day < 10) {
			birthday += "0" + day;
		} else {
			birthday += day;
		}
		// 性别
		var gender = $("input[name='sex']:checked").val();
		// 职业
		var job = $("#job").val();

		var msg = confirmUserInfo(email, userPwd, userPwdConfirm, realName);
		if (msg == true) {
			$.ajax({
						url : userUrl + "/regBaseInfo",
						type : "POST",
						data : {
							'email' : email,
							'userPwd' : userPwd,
							'userPwdConfirm' : userPwdConfirm,
							'realName' : realName,
							'gender' : gender,
							'job' : job,
							'birthday' : birthday
						},
						dataType : "json",
						async : false,
						success : function(response) {
							if (response.success && response.busiSuccess) {
								window.location.href = rootPath
										+ '/pages/user/regist2.jsp';
							} else {
								alert(response.message);
							}
						},
						error : function(response) {
							alert('注册失败');
						}
					});
		} else {
			alert(msg);
		}
	} else {
		alert("请阅读并且同意社圈网服务条款");
	}
}

// 验证用户信息合法性
function confirmUserInfo(email, userPwd, userPwdConfirm, realName) {
	var msg = true;
	if (email == null || email == "") {
		msg = "您还没有填写用户名";
	} else if (email.indexOf("@") < 0) {
		msg = "邮箱注册需要使用正确地址";
	} else if (userPwd == null || userPwd == "") {
		msg = "密码不能为空"
	} else if (userPwdConfirm == null || userPwdConfirm == "") {
		msg = "确认密码不能为空"
	} else if (userPwd != userPwdConfirm) {
		msg = "密码与确认密码不一致";
	} else if (realName == null || realName == "") {
		msg = "真实姓名不能为空"
	}
	return msg;
}

// 验证邮箱是否可用
function isEmailReg() {
	var email = $('#email').val();
	var atIndex=email.indexOf("@");
	var codeIndex=email.lastIndexOf(".");
	if (atIndex < 0||codeIndex<0||atIndex>codeIndex) {
		msg = "邮箱注册需要使用正确地址";
		alert(msg);
		return;
	}
	$.ajax({
				url : userUrl + "/isEmailReg",
				type : "POST",
				data : {
					'email' : email
				},
				dataType : "json",
				async : false,
				success : function(response) {
					if (response.success && response.busiSuccess) {

					} else {
						alert(response.message);
					}
				},
				error : function(response) {
					alert("连接服务器失败");
				}
			});
}